<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>async结合AJAX</title>
  </head>
  <body>
    <script>
      function sendAJAX(url) {
        return new Promise((resolve, reject) => {
          // 1.实例化XML
          const XML = new XMLHttpRequest();
          // 2.建立请求
          XML.open("GET", url);
          // 3.发送请求
          XML.send();
          // 4.事件监听
          XML.onreadystatechange = function () {
            if (XML.readyState === 4) {
              if (XML.status >= 200 && XML.readyState < 300) {
                // 成功
                resolve(XML.response);
              } else {
                // 失败
                reject(XML.status);
              }
            }
          };
        });
      }

      // 测试
      async function main() {
        let result = await sendAJAX("https://api.apiopen.top/getJoke");
        console.log(result);
      }

      main();
    </script>
  </body>
</html>
